package com.tedu.micro.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.micro.service.pojo.param.CartAddParam;
import com.tarena.micro.service.pojo.po.CartPO;
import com.tedu.micro.service.mapper.CartMapper;
import com.tedu.micro.service.service.CartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 是购物车业务的实现
 */
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Override
    public Boolean addCart(CartAddParam param) {
        System.out.println("注入到service的mapper:"+ cartMapper.getClass().getName());
        //1.将入参param 写入数据 转成po(po 和持久层强绑定的)
        CartPO po=new CartPO();
        BeanUtils.copyProperties(param,po);
        //2.调用持久层
        int result = cartMapper.insert(po);
        return result==1;//insert返回值 是1 就是新增成功 0就是新增失败
    }

    @Override
    public Boolean deleteCart(String productCode, String userId) {
        //delete from cart_tbl where product_code=#{} and user_id=#{}
        QueryWrapper queryWrapper=new QueryWrapper();//专门拼接where条件的 update delete select
        queryWrapper.eq("product_code",productCode);
        queryWrapper.eq("user_id",userId);
        int rows=cartMapper.delete(queryWrapper);//传递一个po对象 底层会使用po对象的非空属性拼接 where条件
        return rows>=1;
    }
}
